/**
 * Copyright (C) Octlan Corporation. All rights reserved.
 * 
 * @author todzhang
 * @date 2010-2-25
 * @version $Id: ContentTestActivity.java 15 2010-03-05 07:36:19Z todzhang $
 */
package com.octlan.droid.mii.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.octlan.droid.mis.vo.People.Person;

public class ContentTestActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		insertRecord("tod zhang", "加班中.....");
		displayRecords();
	}

	private void insertRecord(String name, String motion) {
		ContentValues values = new ContentValues();
		values.put(Person.NAME, name);
		values.put(Person.MOTION, motion);
		values.put(Person.LAST_UPDATE, (int) (System.currentTimeMillis() / 1000));
		getContentResolver().insert(Uri.parse(Person.CONTENT_STR), values);
	}

	private void displayRecords() {
		Toast.makeText(this, "Just for fun!", Toast.LENGTH_LONG).show();
		String columns[] = new String[] { Person._ID, Person.NAME, Person.MOTION, Person.LAST_UPDATE };
		Cursor cur = managedQuery(Uri.parse(Person.CONTENT_STR), columns, null, null, null);

		if (cur.moveToFirst()) {
			String id = null;
			String name = null;
			String motion = null;
			CharSequence lastupdate = null;
			do {
				id = cur.getString(cur.getColumnIndex(Person._ID));
				name = cur.getString(cur.getColumnIndex(Person.NAME));
				motion = cur.getString(cur.getColumnIndex(Person.MOTION));
				long  last = (long)cur.getInt(cur.getColumnIndex(Person.LAST_UPDATE)) * 1000L;
				lastupdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(new Date(last));
				//lastupdate = new Date(last).toString();
				
				Toast.makeText(this, id + " " + name +" " + motion + ", update by " + lastupdate, Toast.LENGTH_LONG)
						.show();
			} while (cur.moveToNext());
		}
	}

}
